<?php
namespace Quality\Start\App\Action;

use \Symfony\Component\HttpFoundation\Request;
use \Twig_Environment;
use \Twig_Loader_Filesystem;
use \Symfony\Component\HttpFoundation\Response;

class ErrorHandler
{
    public function onKernelRequest(Request $request)
    {
        if ($request->attributes->has('exception')) {
            $exception = $request->attributes->get('exception');
        }

        $loader = new Twig_Loader_Filesystem('../tpl/Quality/Start/App/View');
        $twig = new Twig_Environment($loader, array('cache' => '../tmp/tpl'));
        $message = 'Erro: ' . $exception->getMessage() . '. With code: ' . $exception->getCode();
        $response = new Response();
        $response->setContent(
            $twig->render(
                'exception.html',
                array(
                    'message' => $message,
                    'baseUrl' => 'http://localhost/workspace/startproject',
                    'error' => $exception->getTrace()
                )
            )

        );
        $response->setStatusCode($exception->getStatusCode());
        $response->headers->replace($exception->getHeaders());
        return $response;
    }
}

